package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyCommentQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.*;
import cn.wolfcode.trip.base.util.JasonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private ITravelService travelService;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private IPraiseService praiseService;
    @Autowired
    private IMyMessageRelationService myMessageRelationService;

    /*
    获取用户的系统通知的总数
    参数：
    资源：/users/{id}/systemmessages/total
    动作：GET
     */
    @GetMapping("/{id}/systemmessages/total")
    public Object getSystemMessageTotalByUserId(@PathVariable Long id){
        return myMessageRelationService.getSystemMessageTotalByUserId(id);
    }

    /*
    获取这个用户的所有系统通知
    参数：
    资源：/{id}/systemmessages/total
    动作：GET
     */
    @GetMapping("/{id}/systemmessages")
    public Object getSystemMessageByUserId(@PathVariable Long id){
        return myMessageRelationService.getSystemMessageByUserId(id) ;
    }


    /*
    获取当前用户的私信息
    资源：/users/{id}/mymessagerelations/sms
    参数：type
    动作：GET
     */
    @GetMapping("/{id}/mymessagerelations/sms")
    public Object getSmsByUserId(@PathVariable Long id,Integer type){
        return myMessageRelationService.getSmsByUserId(id,type);
    }
    /*
    获取用户评论的信息和回复的信息
     */
    @GetMapping("/{id}/mymessagerelations/comments/comments")
    public Object getCommentAndCommentByUserId(@PathVariable Long id,Integer type){
        return myMessageRelationService.getCommentAndCommentByUserId(id,type);
    }

    /*
    查到这个用户的信息中的被评论的游记的信息
    资源：/users/{id}/mymessagerelations/comments/travel
    参数：TYPE
    动作：GET
     */
    @GetMapping("/{id}/mymessagerelations/comments/travel")
    public Object getTravelAndUserByComment(@PathVariable Long id,Integer type){
        return myMessageRelationService.getTravelAndUserByComment(id,type);
    }

    /*
    查询这个用户的被点赞的游记的信息
    资源：/users/{id}/mymessagerelations/{type}/travel
    动作：get
    参数：
     */
    @GetMapping("/{id}/mymessagerelations/{type}/travel")
    public Object getLikeTravelMessages(@PathVariable Long id,@PathVariable Integer type){
        return myMessageRelationService.getLikeTravelMessageByUserId(id,type);
    }





    /*
    获取这个用户的私信总数
    参数：
    资源：/users/{id}/sms/total
    动作：GET
     */
    @GetMapping("/{id}/sms/total")
    public Object getSmsTotalByUserId(@PathVariable Long id){
        return myMessageRelationService.getSmsTotalByUserId(id);
    }

    /**
     * 查询这个用户的被评论的消息总数
     */

    @GetMapping("/{id}/comment/total")
    public Object getCommentAndTravelCommentTotalByUserId(@PathVariable Long id){
        return myMessageRelationService.getCommentTotalByUserId(id);
    }

    /**
     * 查询这个用户的所有的被点赞的游记的总数
     * 参数：
     * 资源：/users/{id}/praises
     * 动作：GET
     */
    @GetMapping("/{id}/praises")
    public Object getPraisesByUserId(@PathVariable Long id){
        return praiseService.getPraisesByUserId(id);
    }



    /**
     * 修改某个用户的游记
     * 资源：/users/{autoId}/travels
     * 动作：PUT
     * 参数：用整个游记对象去接收即可
     */
    @PutMapping("/{autoId}/travels")
    public Object updateTravels(Travel travel) {
        JasonResult js = new JasonResult();
        try {
            //老套路了，修改或新增成功就可以了，失败就返回json提示一下
            travelService.saveOrUpdate(travel);
        } catch (Exception e) {
            js.mark(e.getMessage());
        }
        return js;
    }



    /**
     * 新增某个用户的游记
     * 资源：/users/{autoId}/travels
     * 动作：post
     * 参数：用整个游记对象去接收即可
     */
    @PostMapping("/{autoId}/travels")
    public Object saveTravels(Travel travel) {
        JasonResult js = new JasonResult();
        try {
            //在新增之前要设置当前用户为作者,这个只能在表现层进行，因为这里面用到了mvc中的东西
            travel.setAuthor(UserContext.getCurrentUser());//整个用户给我怼进去
            //老套路了，修改或新增成功就可以了，失败就返回json提示一下
            travelService.saveOrUpdate(travel);
        } catch (Exception e) {
            js.mark(e.getMessage());
        }
        return js;
    }


    /**
     * 获取某个用户的游记
     * 资源：/users/{autoId}/travels
     * 动作：GET
     * 参数：currentPage,当前页
     */
    @GetMapping("/{autoId}/travels")
    public Object updateUser(TravelQueryObject travelQueryObject) {
        travelQueryObject.setOrderBy("t.lastUpdateTime desc");
        PageInfo<Travel> pageInfo = travelService.query(travelQueryObject);
        return pageInfo;
    }




    /**
     * 资源：/users
     * 动作：post
     * 参数：用户必填的信息，email  password  nickName
     */
    @PostMapping
    public Object saveUser(User u){
        JasonResult js = new JasonResult();
        try{
            userService.register(u);
            js.setData(u);
        }catch(Exception e){
            js.mark(e.getMessage());
        }
        return js ;
    }
    /**
     * 资源：/users/{id}
     * 动作：PUT
     * 参数：用户必填的信息，gender  place  nickName sign coverImgUrl headImgUrl
     */
    @PutMapping("/{id}")
    public Object updateUser(User u){
        JasonResult js = new JasonResult();
        try{
            userService.saveOrUpdate(u);
            js.setData(u);
        }catch(Exception e){
            js.mark(e.getMessage());
        }
        return js ;
    }

    /**
     * 当前用户关注其他用户
     * @param userId 被关注用户Id'
     *fansId为当前登录的用户
     * */
    @PostMapping("/{userId}/concern")
    public Object concern(@PathVariable Long userId){
        JasonResult jasonResult = new JasonResult();
        try {
            userService.concern(userId);
        } catch (Exception e) {
            e.printStackTrace();
            //返回错误信息
            jasonResult.mark(e.getMessage());
        }
        return jasonResult;
    }

    /**
     * 判断用户是否已经被当前登录用户关注
     * @param userId
     * @return
     */
    @GetMapping("/{userId}/isConcern")
    public Object isConcern(@PathVariable Long userId){
         return userService.isConcern(userId);
    }

    /**
     * 获取用户的粉丝或者关注
     * @param id 用户id
     * @param type 粉丝/关注
     * @return
     */
    @GetMapping("/{id}/fansOrWatchs")
    public Object getfansOrWatchs(@PathVariable Long id,String type){
        List<User> list = userService.getfansOrWatchs(id, type);
        return list;
    }

    /**
     * 获取某个用户的信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Object getUserData(@PathVariable Long id){
        return userService.getUesrData(id);
    }



    /**
     * 获取用户所有点评
     * @param qo
     * @return
     */
    @GetMapping("/{authorId}/strategyComments")
    public Object strategyComments(StrategyCommentQueryObject qo){
        //不分页
        PageInfo pageInfo =  strategyCommentService.selectForList(qo);
        return pageInfo;
    }
}
